package mbp.tests;

import java.io.StringReader;
import java.io.StringWriter;
import java.util.Iterator;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import mbp.base.FeatureType;
import mbp.base.ResourceType;
import mbp.core.Resource;
import mbp.core.ResourceTypeBuilder;

public class XmlTests 
{
	public XmlTests()
	{
		
	}
	
	public String ResourceToXmlStr(Resource resource, String id)
	{
		String result = null;
		String rType = resource.getType().getResourceTypeName();
		String rOwner = resource.getOwner();
		String rvDade  = resource.getValidDateStr();
		
		try
		{
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.newDocument();
			   
			Element rootElement = doc.createElement("resource");
			doc.appendChild(rootElement);
			
			Element resourceId = doc.createElement("resourceid");
			resourceId.setTextContent(id);
			
			Element resourceType = doc.createElement("resourcetype");
			resourceType.setTextContent(rType);
			
			Element owner = doc.createElement("owner");
		    owner.setTextContent(rOwner);
			 
			Element vDate = doc.createElement("vdate");
			vDate.setTextContent(rvDade);
			
			Element features = doc.createElement("features");
			
			Iterator<String> it = resource.getType().getFeaturesCollection().keySet().iterator();
			while(it.hasNext())
			{
				String key  = it.next();
				Element feature = doc.createElement("feature");
				feature.setTextContent(key);
				 
				FeatureType type = resource.getFeatureType(key);
				Object ob = resource.getFeatureValue(key);
				switch(type)
				{
				case INT:
					Integer valuei = (Integer)ob;
					//hm.put(key, valuei.toString());
					feature.setAttribute("value", valuei.toString());
					break;
				case FLOAT:
					Float valuef = (Float)ob;
					feature.setAttribute("value", valuef.toString());
					break;
				case ENUM:
					Enum<?> valuee = (Enum<?>)ob;
					feature.setAttribute("value", valuee.toString());
					break;
				}
				
				features.appendChild(feature);
			}
			    
			rootElement.appendChild(resourceId);
			rootElement.appendChild(resourceType);
			rootElement.appendChild(owner);
			rootElement.appendChild(vDate);
			rootElement.appendChild(features);
			
			TransformerFactory tf = TransformerFactory.newInstance();
		    Transformer transformer = tf.newTransformer();
		    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
		    StringWriter writer = new StringWriter();
		    transformer.transform(new DOMSource(doc), new StreamResult(writer));
		    String output = writer.getBuffer().toString().replaceAll("\n|\r", "");
		    
		    result = output;
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		
		return result;
	}
	
	public Resource XmlStrToResource(String xmlStr)
	{
		Resource resource = null;
		try
		{
			DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			InputSource is = new InputSource();
			is.setCharacterStream(new StringReader(xmlStr));
			Document doc = db.parse(is);

			//NodeList resourceList = doc.getChildNodes();
			NodeList resourceList = doc.getElementsByTagName("resource");
			Element resourceEl = (Element)resourceList.item(0);
			//System.out.println(resourceEl.getNodeName());
			
			NodeList resourcetype = resourceEl.getElementsByTagName("resourcetype");
			//System.out.println(resourcetype.item(0).getTextContent());
			
			ResourceTypeBuilder rtb = new ResourceTypeBuilder("Resources.xml");
			ResourceType resourceTypeExt = rtb.GetResourceType(resourcetype.item(0).getTextContent());
			resource = new Resource(resourceTypeExt);
			
			NodeList resourceid = resourceEl.getElementsByTagName("resourceid");
			//System.out.println(resourceid.item(0).getTextContent());
			
			NodeList owner = resourceEl.getElementsByTagName("owner");
			//System.out.println(owner.item(0).getTextContent());
			resource.setOwner(owner.item(0).getTextContent());
			
			NodeList vdate = resourceEl.getElementsByTagName("vdate");
			//System.out.println(vdate.item(0).getTextContent());
			resource.setValidDateStr(vdate.item(0).getTextContent());
			
			NodeList featuresNode = resourceEl.getElementsByTagName("features");
			//System.out.println(featuresNode.item(0).getTextContent());
			
			Element featuresEl = (Element)featuresNode.item(0);
			NodeList features = featuresEl.getElementsByTagName("feature");
			
			for(int i = 0; i<features.getLength(); i++)
			{
				Element feature = (Element) features.item(i);
				String key = feature.getTextContent();
				String value = feature.getAttribute("value");
				//System.out.println(feature.getTextContent() + "  " + feature.getAttribute("value"));
				
				FeatureType type = resource.getFeatureType(key);
				
				switch(type)
				{
				case INT:
					Integer valuei = Integer.parseInt(value);
					resource.setFeatureValue(key, valuei);
					break;
				case FLOAT:
					Float valuef = Float.parseFloat(value);
					resource.setFeatureValue(key, valuef);
					break;
				case ENUM:
					//Class cl = Class.forName(resourceTypeExt.GetEnumClassName(key));
					//Enum<?> valuee = Enum.valueOf(cl, value);
					String valuee = value;
					resource.setFeatureValue(key, valuee);
					break;
				}
			}
			
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		return resource;
	}

}
